package com.tim.app.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.application.library.widget.CropImageView;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;
import com.tim.app.manager.ImagePickerManager;
import com.tim.app.ui.widget.imagepick.bean.ImageItem;

import java.io.File;
import java.util.ArrayList;

public class ImageCropActivity extends AppCompatActivity implements View.OnClickListener, CropImageView.OnBitmapSaveCompleteListener {

    private ImageButton ripple_back;
    private TextView ripple_finish;
    private CropImageView mCropImageView;
    private Bitmap mBitmap;
    private ArrayList<ImageItem> mImageItems;
    private boolean mIsSaveRectangle;
    private int mOutputX;
    private int mOutputY;
    private String imagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        ripple_back = (ImageButton) findViewById(R.id.ripple_back);
        ripple_finish = (TextView) findViewById(R.id.ripple_finish);
        mCropImageView = (CropImageView) findViewById(R.id.cv_crop_image);
        mCropImageView.setOnBitmapSaveCompleteListener(this);

        ripple_back.setOnClickListener(this);
        ripple_finish.setOnClickListener(this);

        mImageItems = ImagePickerManager.ins().getSelectedImages();
        mOutputX = ImagePickerManager.ins().getOutPutX();
        mOutputY = ImagePickerManager.ins().getOutPutY();
        mIsSaveRectangle = ImagePickerManager.ins().isSaveRectangle();
        imagePath = getIntent().getStringExtra(AppKey.IMAGE_PICK_PATH);

        mCropImageView.setFocusStyle(ImagePickerManager.ins().getStyle());
        mCropImageView.setFocusWidth(ImagePickerManager.ins().getFocusWidth());
        mCropImageView.setFocusHeight(ImagePickerManager.ins().getFocusHeight());

        showPic();
    }

    private void showPic(){
        //缩放图片
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        options.inSampleSize = calculateInSampleSize(options, displayMetrics.widthPixels, displayMetrics.heightPixels);
        options.inJustDecodeBounds = false;
        mBitmap = BitmapFactory.decodeFile(imagePath, options);
        mCropImageView.setImageBitmap(mBitmap);
    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int width = options.outWidth;
        int height = options.outHeight;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = width / reqWidth;
            } else {
                inSampleSize = height / reqHeight;
            }
        }
        return inSampleSize;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ripple_back:
//                if (!TextUtils.isEmpty(imagePath)) {
//                    EventManager.ins().sendEvent(EventTag.NOTIFY_CHOOSE_PHOTO, 0, 0, imagePath);
//                }
                finish();
                break;
            case R.id.ripple_finish:
                mCropImageView.saveBitmapToFile(ImagePickerManager.ins().getCropCacheFolder(this), mOutputX, mOutputY, mIsSaveRectangle);
                break;
        }
    }


    @Override
    public void onBitmapSaveSuccess(File file) {
//        Toast.makeText(ImageCropActivity.this, "裁剪成功:" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        EventManager.ins().sendEvent(EventTag.NOTIFY_CHOOSE_PHOTO, 0, 0, file.getAbsolutePath());
        if (mImageItems != null && mImageItems.size() > 0)
            mImageItems.remove(0);
        ImageItem imageItem = new ImageItem();
        imageItem.path = file.getAbsolutePath();
        mImageItems.add(imageItem);
        Intent intent = new Intent();
        intent.putExtra(ImagePickerManager.EXTRA_RESULT_ITEMS, mImageItems);
        setResult(ImagePickerManager.RESULT_CODE_ITEMS, intent);   //单选不需要裁剪，返回数据
        finish();
    }

    @Override
    public void onBitmapSaveError(File file) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mBitmap && !mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }

}
